/**
 * Copyright (c) 2011 Nokia Corporation.
 */

#ifndef BACKUPENGINE_H
#define BACKUPENGINE_H

// Qt includes
#include <QFileInfo>
#include <QList>
#include <QObject>
#include <QString>

// Forward declarations
class DriveModel;
class FileManager;
class FileModel;
class QAbstractListModel;


class BackupEngine : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString sourcePath READ sourcePath NOTIFY sourcePathChanged)
    Q_PROPERTY(QString targetDrive READ targetDrive NOTIFY targetDriveChanged)
    Q_PROPERTY(int numberOfMarkedFiles READ numberOfMarkedFiles NOTIFY numberOfMarkedFilesChanged)
    Q_PROPERTY(qlonglong sizeOfMarkedFiles READ totalSizeOfMarkedFiles NOTIFY totalSizeOfMarkedFilesChanged)
    Q_PROPERTY(int backupProgressState READ backupProgressState NOTIFY backupProgressStateChanged)

public: // Data types

    enum BackupProgressState {
        Idle = 0, // Nothing to do
        InProgress,
        Failed,
        Cancelled,
        Finished
    };

public: // Construction and destruction
    BackupEngine(QObject *parent = 0);
    virtual ~BackupEngine();

public:
    QAbstractListModel *sourceFileModel() const;
    QAbstractListModel *driveModel() const;
    QString sourcePath() const;
    QString targetDrive() const;
    int numberOfMarkedFiles() const;
    qlonglong totalSizeOfMarkedFiles();
    int backupProgressState() const;

public slots:
    void refreshDrives();
    void setSourceDrive(int index);
    void setTargetDrive(int index);
    void selectSourceFileModelIndex(int index);
    void navigateToSourceFolderByIndex(int index);
    bool navigateToSourceParentFolder();
    void clearMarkedFiles();
    bool startBackup();

private slots:
    void refreshBackedUpFiles(const QString &driveVolume = QString());
    void refreshSourceFileModelStatuses();
    void setBackupProgressState(BackupProgressState state = Idle);
    void resetBackupProgressState();
    void handleCopyStatus(int index,
                          qlonglong totalSizeCopied,
                          const QString &fileCopied,
                          const QString &fileStarting);
    void handleCopyFailed(QFile::FileError error, const QString &filename);
    void handleBackupCancelled();
    void handleBackupFinished();

private:
    QString targetRootPath(const QString &driveVolume = QString()) const;

signals:
    void sourcePathChanged(QString path);
    void targetDriveChanged(QString drive);
    void numberOfMarkedFilesChanged(int count);
    void totalSizeOfMarkedFilesChanged(qlonglong size);
    void backupProgressStateChanged(int state);
    void progress(int value);
    void cancelBackup();

private: // Data
    FileManager *m_fileManager; // Owned
    FileModel *m_sourceFileModel; // Owned
    DriveModel *m_driveModel; // Owned
    QString m_currentSourcePath;
    QString m_targetDrive;
    QFileInfoList m_markedFiles;
    QFileInfoList m_backedUpFiles;
    int m_backupProgressState;
    qlonglong m_cachedTotalSize;
};


#endif // BACKUPENGINE_H

// End of file.
